package com.tool.cn.util;

import java.time.DayOfWeek;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;


/**
 * 
 * @author wangzonghui
 * @date 2021-08-04 15:26:38
 * @Description java8 LocalTime 时间工具类
 */
public class TimeUtil {
	
	/**
	 * 获取当前时间 格式 yyyy-MM-dd
	 * @return
	 */
	public static String getTime() {
		return LocalDate.now().toString();
	}
	
	/**
     * 将long类型毫秒时间戳转指定格式字符串
     * @param time 毫秒时间戳
     * @param format 字符串格式
     */
    public static String timeToString(Long time, String format){
        DateTimeFormatter formatString = DateTimeFormatter.ofPattern(format);
        return formatString.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()));
    }
    
    /**
     * 将字符串格式为：yyyy-MM-dd HH:mm:ss 转毫秒时间戳
     * @param time 默认格式时间
     */
    public static Long timeToLong(String time) {
        DateTimeFormatter formatString = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime parse = LocalDateTime.parse("2019-11-28 08:52:50", formatString);
        return LocalDateTime.from(parse).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

	/**
	 * 获取当前秒级UTC时间
	 * @return
	 */
	public static Long getUTC(){
		return System.currentTimeMillis()/1000;
	}
	
    /**
     * 将localDate 格式化为指定格式字符串
     * @param localDate 本地时间
     * @param format 时间字符串格式 yyyy-MM-dd HH:mm:ss
     */
    public static String localDateFormat(LocalDate localDate, String format){
        // ""
        String localDateStr = TimeUtil.timeToString(localDate2Second(localDate), format);
        return localDateStr;
    }

    /**
     * localDateTime 转指定格式字符串
     * @param localDateTime
     * @param  format 字符串格式
     */
    public static String localDateTimeFormat(LocalDateTime localDateTime, String format){
        // "yyyy-MM-dd HH:mm:ss"
        String localDateTimeStr = TimeUtil.timeToString(localDateTime2Second(localDateTime), format);
        return localDateTimeStr;
    }
    
    /**
     * 取本月第一天
     */
    public static LocalDate firstDayOfThisMonth() {
        LocalDate today = LocalDate.now();
        return today.with(TemporalAdjusters.firstDayOfMonth());
    }
    
    /**
     * 取本月第一天
     */
    public static LocalDate firstDayOfLastMonth() {
        LocalDate today = LocalDate.now();
        today = today.minusMonths(1);
        return today.with(TemporalAdjusters.firstDayOfMonth());
    }
    
    /**
     * 取本月第N天
     */
    public static LocalDate dayOfThisMonth(int n) {
        LocalDate today = LocalDate.now();
        return today.withDayOfMonth(n);
    }
    
    /**
     * 取本月最后一天
     */
    public static LocalDate lastDayOfThisMonth() {
        LocalDate today = LocalDate.now();
        return today.with(TemporalAdjusters.lastDayOfMonth());
    }
    
    /**
     * 获取本周一
     * @param date 毫秒时间戳
     */
    public static LocalDateTime firstDayOfWeek(Long date) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date), ZoneId.systemDefault());
        return localDateTime.with(DayOfWeek.MONDAY);
    }
    /**
     * 获取上周一
     * @param date 毫秒时间戳
     */
    public static LocalDateTime firstDayOfLastWeek(Long date) {
        // long转LocalDateTime
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date), ZoneId.systemDefault());
        LocalDateTime localDateTime1 = localDateTime.plusDays(-7);
        return localDateTime1.with(DayOfWeek.MONDAY);
    }
    /**
     * 获取上周一
     * @param date 秒时间戳
     */
    public static LocalDateTime lastDay(Long date) {
        // long转LocalDateTime
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(date), ZoneId.systemDefault());
        LocalDateTime localDateTime1 = localDateTime.plusDays(-1);
        return localDateTime1;
    }
    /**
     * 取本月第一天的开始时间
     */
    public static LocalDateTime startOfThisMonth() {
        return LocalDateTime.of(firstDayOfThisMonth(), LocalTime.MIN);
    }
    /**
     * 取本月最后一天的结束时间
     */
    public static LocalDateTime endOfThisMonth() {
        return LocalDateTime.of(lastDayOfThisMonth(), LocalTime.MAX);
    }
    /**
     *  LocalDate转时间戳 
     *  */
    public static Long localDate2Second(LocalDate localDate) {
        return LocalDateTime.of(localDate, LocalTime.MIN).toInstant(ZoneOffset.ofHours(8)).getEpochSecond();
    }
    
    /**
     * LocalDate转时间戳 
     * */
    public static Long localDateTime2Second(LocalDateTime localDateTime) {
        return localDateTime.toEpochSecond(ZoneOffset.of("+8"));
    }

}
